Function Reference

FileOpen

Opens a text file for reading or writing.

FileOpen ( "filename", mode )

 

Parameters

filename Filename of the text file to open.
mode Mode (read or write) to open the file in.
Can be a combination of the following:
  0 = Read mode
  1 = Write mode (append to end of file)
  2 = Write mode (erase previous contents)
  4 = Read raw mode
  8 = Create directory structure if it doesn't exist (See Remarks).
Both write modes will create the file if it does not already exist. The folder path must already exist (except using mode '8' - See Remarks).

 

Return Value

Success: Returns a file "handle" for use with subsequent file functions.
Failure: Returns -1 if error occurs.

 

Remarks

Up to 64 files can be open simultaneously by one AutoIt script. Exceeding this limit throws a run-time error.
A file can only be in either read or write mode; it cannot be in both.
When opening a file in write mode, the file will be created if it does not exist.
When finished working with a file, call the FileClose function to close the file. Autoit normally closes all files upon termination, but explicitly calling FileClose is still a good idea.

When using the mode=4 (Raw Read) the filename is defined as "\\.\A:" for reading sector on a floppy disk the count must be a multiple of sector size(512).

Non-existing directory structure:
------------------------------------
By default the file will not be created if the directory structure doesn't exist.
To overwrite this behaviour use the modes '1' or '2' together with mode-flag '8'!
For instance the mode-flag '9' (1 + 8) checks for the directory structure and if it doesn't exist creates it automatically and then opens the file for appending.

 

Related

FileClose, FileReadLine, FileWriteLine, FileRead

 

Example


$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileClose($file)


; Another sample which automatically creates the directory structure
$file = FileOpen("test.txt", 10) ; which is similar to 2 + 8 (erase + create dir)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileClose($file)